#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "maz/assert.h"
#include "riff_wav.h"

int dlvl = DBUG;

int main(int argc, char **argv)
{
    int ret = 0;
    int format = 0;
    int samplerate = 0;
    int bitdepth = 0;
    int channel =0 ;
    int len = 0;
    WAVE_INFO info = { 0 };
    WAVE_HEADER header = { 0 };
    const char s[2] = ".";

    char        iname[32];
    FILE       *ifile       = NULL;
    int         ircnt       = 0;

    /* 打印函数使用方法 */
    if (argc != 2)
    {
        printf("usage   : %s <wav file name>\n", argv[0]);
        printf("eg      : %s dayu_8k_1ch_s16le.wav \n", argv[0]);
        printf("eg      : %s dayu_16k_1ch_s16le.wav \n", argv[0]);
        printf("\n");
        return -1;
    }

    /* 解析输入参数得到文件名 */
    strcpy(iname, argv[1]);

    /* 获取输入文件大小, 计算输出文件大小 */
    len = sizeof(WAVE_HEADER);

    /* 读取输出数据 */
    ifile = fopen(iname, "r");
    ASSERT_RETVAL(NULL == ifile, -1, "err: can not open %s file", iname);

    ircnt = fread(&header, 1, len, ifile);
    ASSERT_RETVAL(ircnt != len, ret, "err: fread, rcnt = %d", ircnt);
    fclose(ifile);

    dmsg(DBUG, "samplerate = %-8d %s\n", header.subchunk1.samplerate, RIFF_WAV_SAMPLERATE_STR(header.subchunk1.samplerate));
    dmsg(DBUG, "bitdepth   = %-8d %s\n", header.subchunk1.bitdepth, RIFF_WAV_BITDEPTH_STR(header.subchunk1.bitdepth));
    dmsg(DBUG, "channal    = %-8d %s\n", header.subchunk1.channel, RIFF_WAV_CHANNEL_STR(header.subchunk1.channel));
    dmsg(DBUG, "byterate   = %-8d %dbps\n", header.subchunk1.byterate, header.subchunk1.byterate);
    dmsg(DBUG, "blockalign = %-8d %d\n", header.subchunk1.blockalign, header.subchunk1.blockalign);
    dmsg(DBUG, "foramtcode = %-8d %s\n", header.subchunk1.format, RIFF_WAV_FORMAT_STR(header.subchunk1.format));
}

